//
//  MLWTipsICookVideoDetailViewController.m
//  KITCHEN_STORIES
//
//  Created by 孟令通 on 16/6/8.
//  Copyright © 2016年 MLW. All rights reserved.
//

#import "MLWTipsICookVideoDetailViewController.h"

#import "MLWTipsICookVideoDetailModel.h"

#import "MLWTipsICookMaterialModel.h"

#import "MLWTipsICookKnoledgeModel.h"

#import "MLWTipsICookOtherModel.h"

#import "MLWTipsICookDetailMainCell.h"

#import "MLWTipsICookTagCell.h"

#import "MLWLTVideoPlayerViewController.h"

@interface MLWTipsICookVideoDetailViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, ICookVideoDetailDelegate, TipsICookTagDelegate>

/** 做法链接 */
@property (nonatomic, copy) NSString *detailHowUrl;

/** 食材链接 */
@property (nonatomic, copy) NSString *detailFoodUrl;

/** 相关知识链接 */
@property (nonatomic, copy) NSString *detailKnowledgeUrl;

/** 相宜相克链接 */
@property (nonatomic, copy) NSString *detailOtherUrl;

/** 主集合视图 */
@property (nonatomic, retain) UICollectionView *mainCollectionView;

/** 主页面及步骤数组 */
@property (nonatomic, retain) NSMutableArray *mainStepModelArray;

@property (nonatomic, retain) NSMutableArray *materialModelArray;

@property (nonatomic, retain) NSMutableArray *knoledgeModelArray;

@property (nonatomic, retain) NSMutableArray *otherModelArray;

@property (nonatomic, assign) CGFloat itemHeight;

@property (nonatomic, retain) UICollectionViewFlowLayout *flowLayout;

@property (nonatomic, retain) MBProgressHUD *waitingHUD;

@end

@implementation MLWTipsICookVideoDetailViewController

- (void)dealloc
{
    [_mainCollectionView release];
    [_materialModelArray release];
    [_knoledgeModelArray release];
    [_otherModelArray release];
    [_flowLayout release];
    [_waitingHUD release];
    
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    
}

-(void)loadView
{
    [super loadView];
    
    self.waitingHUD = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
    
    self.waitingHUD.mode = MBProgressHUDModeIndeterminate;
    
    self.waitingHUD.labelText = @"玩命加载中...";
    
    self.waitingHUD.progress = 0.3f;
    
    self.waitingHUD.animationType = MBProgressHUDAnimationZoom;

    
    self.itemHeight = ScreenHeight * 10;
    
    self.detailHowUrl = ICOOKING_VIDEO_DETAIL_HOW_BODY(self.cateId);
    
    self.detailFoodUrl = ICOOKING_VIDEO_DETAIL_FOOD_BODY(self.cateId);
    
    self.detailKnowledgeUrl = ICOOKING_VIDEO_DETAIL_KNOWLEDGE_BODY(self.cateId);
    
    self.detailOtherUrl = ICOOKING_VIDEO_DETAIL_OTHER_BODY(self.cateId);
    
    [self askForHowDataWithUrl:ICOOKING_URL Body:self.detailHowUrl];
    
    [self askForFoodDataWithUrl:ICOOKING_URL Body:self.detailFoodUrl];
    
    [self askForKnoledgeDataWithUrl:ICOOKING_URL Body:self.detailKnowledgeUrl];
    
    [self askForOtherDataWithUrl:ICOOKING_URL Body:self.detailOtherUrl];
    
    // 初始化数据源数组
    self.mainStepModelArray = [NSMutableArray array];
    
    self.materialModelArray = [NSMutableArray array];
    
    self.knoledgeModelArray = [NSMutableArray array];
    
    self.otherModelArray = [NSMutableArray array];
    
    [self createMainCollectionView];
}

-(void)askForHowDataWithUrl:(NSString *)url Body:(NSString *)body
{
    [MLWNetTool PostNetWithUrl:url body:body bodyStyle:MLWBodyString header:nil response:MLWJSON success:^(id result) {
        
        
        
        NSDictionary *dictionary = [(NSDictionary *)result objectForKey:@"data"];
        
        MLWTipsICookVideoDetailModel *model = [[MLWTipsICookVideoDetailModel alloc] initWithDic:dictionary];
        
        [self.mainStepModelArray addObject:model];
        
        [self.waitingHUD hide:YES afterDelay:0.3];
        
        [self.mainCollectionView reloadData];
        
    } failure:^(NSError *error) {
        
        
        
    }];
}

-(void) askForFoodDataWithUrl:(NSString *)url Body:(NSString *)body
{
    [MLWNetTool PostNetWithUrl:url body:body bodyStyle:MLWBodyString header:nil response:MLWJSON success:^(id result) {
        
        NSDictionary *dictionary = [(NSDictionary *)result objectForKey:@"data"];
        
        MLWTipsICookMaterialModel *model = [[MLWTipsICookMaterialModel alloc] initWithDic:dictionary];
        
        [self.materialModelArray addObject:model];
        [model release];
        
    } failure:^(NSError *error) {
        
        
    }];
}

- (void) askForKnoledgeDataWithUrl:(NSString *)url Body:(NSString *)body
{
    [MLWNetTool PostNetWithUrl:url body:body bodyStyle:MLWBodyString header:nil response:MLWJSON success:^(id result) {
        
        NSDictionary *dictionary = [(NSDictionary *)result objectForKey:@"data"];
        
        MLWTipsICookKnoledgeModel *model = [[MLWTipsICookKnoledgeModel alloc] initWithDic:dictionary];
        
        [self.knoledgeModelArray addObject:model];
        [model release];
        
    } failure:^(NSError *error) {
        
        
        
    }];
}

-(void) askForOtherDataWithUrl:(NSString *)url Body:(NSString *)body
{
    [MLWNetTool PostNetWithUrl:url body:body bodyStyle:MLWBodyString header:nil response:MLWJSON success:^(id result) {
        
        NSDictionary *dictionary = [[(NSDictionary *)result objectForKey:@"data"] objectForKey:@"material"];
        
        MLWTipsICookOtherModel *model = [[MLWTipsICookOtherModel alloc] initWithDic:dictionary];
        
        [self.otherModelArray addObject:model];
        [model release];
        
    } failure:^(NSError *error) {
        
    }];
}

#pragma mark 创建主集合视图
-(void) createMainCollectionView
{
    
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    self.flowLayout.itemSize = CGSizeMake(ScreenWidth, ScreenHeight / 2.0);
    
    self.flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    self.flowLayout.minimumLineSpacing = 7;
    
    self.flowLayout.minimumInteritemSpacing = 0;
    
    self.mainCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, -20, ScreenWidth, ScreenHeight) collectionViewLayout:_flowLayout];
    
    self.mainCollectionView.dataSource = self;
    
    self.mainCollectionView.delegate = self;
    
    self.mainCollectionView.showsVerticalScrollIndicator = NO;
    
    self.mainCollectionView.backgroundColor = [UIColor colorWithRed:0.97 green:0.97 blue:0.97 alpha:1.00];
    
    [self.view addSubview:_mainCollectionView];
    [_mainCollectionView release];
    
    [self.mainCollectionView registerClass:[MLWTipsICookDetailMainCell class] forCellWithReuseIdentifier:@"headerCell"];
    
    [self.mainCollectionView registerClass:[MLWTipsICookTagCell class] forCellWithReuseIdentifier:@"bodyCell"];
}


#pragma mark -
#pragma mark <UICollectionViewDelegate, UICollectionViewDataSource>
#pragma mark 返回每个section中item的数量
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 2;
}

#pragma mark cell重用
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.item == 0) {
        
        MLWTipsICookDetailMainCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"headerCell" forIndexPath:indexPath];
        
        if (self.mainStepModelArray.count != 0 && self.mainStepModelArray != nil) {
            
            MLWTipsICookVideoDetailModel *model = [self.mainStepModelArray objectAtIndex:indexPath.item];
            cell.model = model;
            cell.backgroundColor = [UIColor whiteColor];
            cell.MLWDelegate = self;
        }
        else
        {
            cell.backgroundColor = [UIColor colorWithRed:0.91 green:0.31 blue:0.25 alpha:1.00];
            
        }
        
        return cell;
    }
    
    if (indexPath.item == 1) {
        
        MLWTipsICookTagCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"bodyCell" forIndexPath:indexPath];
        
        cell.tipsTagDelegate = self;
        
//        cell.backgroundColor = [UIColor colorWithRed:0.38 green:0.69 blue:0.89 alpha:1.00];
        
        cell.stepDataArray = self.mainStepModelArray;
        
        cell.materialDataArray = self.materialModelArray;
        
        cell.knowledgeDataArray = self.knoledgeModelArray;
        
        cell.otherDataArray = self.otherModelArray;
        
        return cell;
    }
    
    return nil;
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    self.hidesBottomBarWhenPushed = YES;
}

-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    
    self.hidesBottomBarWhenPushed = NO;
}

#pragma mark 返回每个cell的高度
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.item == 0) {
        
        // 图片高 974
        CGFloat scale = 1338.0 / 750.0;
        
        return CGSizeMake(ScreenWidth, ScreenWidth * scale);
        
    }
    
    if (indexPath.item == 1) {
        
        return CGSizeMake(ScreenWidth, self.itemHeight);
        
    }
    
    return CGSizeMake(ScreenWidth, self.itemHeight);
}




#pragma mark 返回上一页
- (void)goBackToFrontPage
{
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)pushToVideoPlayerControllerWithUrl:(NSString *)url
{
//    MLWTipsICookVideoDetailViewController *selfVC = self;
    
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        
        switch (status)
        {
            case -1:
            {
                //提示框:
                MBProgressHUD *textOnlyHUD = [MBProgressHUD showHUDAddedTo:self.tabBarController.view animated:YES];
                textOnlyHUD.mode = MBProgressHUDModeText;
                textOnlyHUD.labelText = @"未知网络";
                [textOnlyHUD hide:YES afterDelay:1.f];
            }
                break;
            case 0:
            {
                //提示框:
                MBProgressHUD *textOnlyHUD = [MBProgressHUD showHUDAddedTo:self.tabBarController.view animated:YES];
                textOnlyHUD.mode = MBProgressHUDModeText;
                textOnlyHUD.labelText = @"无法连接";
                [textOnlyHUD hide:YES afterDelay:1.f];
            }
                break;
            case 1:
            {
                NSArray *subviewArray = [[[[UIApplication sharedApplication] valueForKeyPath:@"statusBar"] valueForKeyPath:@"foregroundView"] subviews];
                int type = 0;
                for (id subview in subviewArray)
                {
                    if ([subview isKindOfClass:NSClassFromString(@"UIStatusBarDataNetworkItemView")])
                    {
                        type = [[subview valueForKeyPath:@"dataNetworkType"] intValue];
                    }
                    switch (type)
                    {
                        case 1:
                        {
                            //提示框:
                            MBProgressHUD *textOnlyHUD = [MBProgressHUD showHUDAddedTo:self.tabBarController.view animated:YES];
                            textOnlyHUD.mode = MBProgressHUDModeText;
                            textOnlyHUD.labelText = @"当前为2G网络";
                            [textOnlyHUD hide:YES afterDelay:1.f];
                        }
                            break;
                        case 2:
                        {
                            //提示框:
                            MBProgressHUD *textOnlyHUD = [MBProgressHUD showHUDAddedTo:self.tabBarController.view animated:YES];
                            textOnlyHUD.mode = MBProgressHUDModeText;
                            textOnlyHUD.labelText = @"当前为3G网络";
                            [textOnlyHUD hide:YES afterDelay:1.f];
                        }
                            break;
                        case 3:
                        {
                            //提示框:
                            MBProgressHUD *textOnlyHUD = [MBProgressHUD showHUDAddedTo:self.tabBarController.view animated:YES];
                            textOnlyHUD.mode = MBProgressHUDModeText;
                            textOnlyHUD.labelText = @"当前为4G网络";
                            [textOnlyHUD hide:YES afterDelay:1.f];
                        }
                            break;
                            
                        default:
                            break;
                    }
                }
            }
                break;
            case 2:
            {
                //提示框:
                MBProgressHUD *textOnlyHUD = [MBProgressHUD showHUDAddedTo:self.tabBarController.view animated:YES];
                textOnlyHUD.mode = MBProgressHUDModeText;
                textOnlyHUD.labelText = @"当前为WIFI";
                [textOnlyHUD hide:YES afterDelay:1.f];
            }
                break;
                
            default:
            {
                //提示框:
                MBProgressHUD *textOnlyHUD = [MBProgressHUD showHUDAddedTo:self.tabBarController.view animated:YES];
                textOnlyHUD.mode = MBProgressHUDModeText;
                textOnlyHUD.labelText = @"无网络链接";
                [textOnlyHUD hide:YES afterDelay:1.f];
            }
                break;
        }
        
    }];
    
    MLWLTVideoPlayerViewController *videoPlayer = [MLWLTVideoPlayerViewController sharePlayerInstance];

    videoPlayer.url = url;
    
    [self.navigationController pushViewController:videoPlayer animated:YES];
//    [videoPlayer release];

}

-(void)didChagendCellHeightWithHeight:(CGFloat)height
{
    
    self.itemHeight = height;
    
    NSIndexPath *indexpath = [NSIndexPath indexPathForItem:1 inSection:0];
    
    [self.mainCollectionView reloadItemsAtIndexPaths:@[indexpath]];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
